package com.android.tiango.baba.act;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.fragment.FrHome;
import com.android.tiango.baba.fragment.FrUserInfo;
import com.android.tiango.baba.fragment.FraMyBa;
import com.android.tiango.baba.fragment.FraQiangGouba;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;

public class ActHome extends BaBaseActivity implements OnClickListener {
	// private TextView mHomeTextView, mCenterTextView;
	private long mExitTime;
	private View mHomelineView, mCenterlineView, mPersonlineView;
	private ImageView mCenterImageView, mHomeImageView, mBarImageView;
	private LinearLayout mHomeLinearLayout, mPersonLinearLayout;

	public final static int QIANG_GOU_BA = 0;
	public final static int MY_BA = 1;
	public static int WEI_BA_TYPE;
	AnimatorSet animation = null;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_home);
		initView();
	}

	private void initView() {
		// mCenterTextView = (TextView) findViewById(R.id.tv_center);
		// mHomeTextView = (TextView) findViewById(R.id.tv_home);
		mCenterImageView = (ImageView) findViewById(R.id.iv_center);
		mHomeImageView = (ImageView) findViewById(R.id.iv_home);
		mBarImageView = (ImageView) findViewById(R.id.iv_bar);

		mCenterlineView = (View) findViewById(R.id.ve_center);
		mHomelineView = (View) findViewById(R.id.ve_centerleft);
		mPersonlineView = (View) findViewById(R.id.ve_centerright);

		mHomeLinearLayout = (LinearLayout) findViewById(R.id.ll_home);
		mPersonLinearLayout = (LinearLayout) findViewById(R.id.ll_person);

		mPersonLinearLayout.setOnClickListener(this);
		mHomeLinearLayout.setOnClickListener(this);

		mBarImageView.setOnClickListener(this);
		turnToFragment(FrHome.class);

	}

	@Override
	public void onClick(View v) {
		changeBg(v.getId());
		switch (v.getId()) {
		case R.id.ll_home:
			turnToFragment(FrHome.class);
			break;
		case R.id.ll_person:
			turnToFragment(FrUserInfo.class);
			break;
		case R.id.iv_bar:
			initAnimo();
			switch (WEI_BA_TYPE) {
			case QIANG_GOU_BA:
				turnToFragment(FraQiangGouba.class);
				break;
			case MY_BA:
				turnToFragment(FraMyBa.class);

				break;

			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	public void switchFragment(int type) {
		System.out.println("switchFragment");
		this.WEI_BA_TYPE = type;
		switch (type) {
		case QIANG_GOU_BA:
			turnToFragment(FraQiangGouba.class);
			break;
		case MY_BA:
			turnToFragment(FraMyBa.class);
			break;

		default:
			break;
		}
	}

	private void initAnimo() {
		if (animation == null) {
			animation = new AnimatorSet();
			float startY = mBarImageView.getTop();
			float endY = mBarImageView.getTop() - 80f;
			System.out.println("startY:" + startY + " endY:" + endY);
			int duration = 240;
			ObjectAnimator bounceAnim = ObjectAnimator.ofFloat(mBarImageView, "y", startY, endY);
			bounceAnim.setDuration(duration);
			// bounceAnim.setRepeatCount(1);
			// bounceAnim.setRepeatMode(ValueAnimator.REVERSE);
			bounceAnim.setInterpolator(new DecelerateInterpolator());

			ObjectAnimator squashAnim2 = ObjectAnimator.ofFloat(mBarImageView, "scaleX", 1f, 1.3f);
			squashAnim2.setDuration(duration / 4);
			squashAnim2.setRepeatCount(1);
			squashAnim2.setRepeatMode(ValueAnimator.REVERSE);
			squashAnim2.setInterpolator(new AccelerateInterpolator());
			ObjectAnimator stretchAnim1 = ObjectAnimator.ofFloat(mBarImageView, "y", endY, startY);
			stretchAnim1.setDuration(duration);
			// stretchAnim1.setRepeatCount(1);
			stretchAnim1.setInterpolator(new AccelerateInterpolator());
			// stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);
			ObjectAnimator stretchAnim2 = ObjectAnimator.ofFloat(mBarImageView, "scaleY", 1f, 0.7f);
			stretchAnim2.setDuration(duration / 4);
			stretchAnim2.setRepeatCount(1);
			stretchAnim2.setInterpolator(new AccelerateInterpolator());
			stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);
			AnimatorSet bouncer = new AnimatorSet();
			AnimatorSet bouncerScr = new AnimatorSet();
			bouncerScr.play(squashAnim2).with(stretchAnim2).before(bounceAnim);
			AnimatorSet bouncerScr_2 = new AnimatorSet();
			bouncerScr_2.play(stretchAnim1).before(bouncerScr);
			bouncer.play(bounceAnim).before(bouncerScr_2);
			animation.play(bouncer).before(stretchAnim1);
		}

		if (animation.isRunning()) {
			return;
		} else {
			animation.start();
		}
	}

	private void changeBg(int id) {
		mCenterImageView.setBackgroundResource(R.drawable.icon_person);
		mHomeImageView.setBackgroundResource(R.drawable.icon_home);
		mHomelineView.setVisibility(View.GONE);
		mCenterlineView.setVisibility(View.GONE);
		mPersonlineView.setVisibility(View.GONE);
		switch (id) {
		case R.id.ll_home:
			mHomeImageView.setBackgroundResource(R.drawable.icon_home_check);
			mHomelineView.setVisibility(View.VISIBLE);
			break;
		case R.id.ll_person:
			mCenterImageView.setBackgroundResource(R.drawable.ic_person_check);
			mPersonlineView.setVisibility(View.VISIBLE);
			break;
		case R.id.iv_bar:
			mCenterlineView.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {

		if ((System.currentTimeMillis() - mExitTime) > 2000) {
			ToastUtils.showMessage(getResources().getString(R.string.toast_exsit_app));
			mExitTime = System.currentTimeMillis();
		} else {
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					StringUtils.onExit(ActHome.this);
				}
			}, 500);
		}
	}
}
